using HKB.Common;
using System;
using System.Data;
using System.Web;
using System.Web.Security;
public class AdminBll
{
	private Conn conn = null;
	public AdminBll()
	{
		this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
	}
	public AdminBll(Conn _conn)
	{
		this.conn = _conn;
	}
	public DataTable GetAllAdmin()
	{
		return this.conn.GetDataTable("Select * From [HL_Admin]");
	}
	public DataTable GetAdminByAdminID(int AdminID)
	{
		return this.conn.GetDataTable("Select * From [HL_Admin] Where Admin_ID=" + AdminID);
	}
	public DataTable GetAdminUserGroupByAdminID(int AdminID)
	{
		return this.conn.GetDataTable("Select * From [HL_AdminUserGroup] Where [AdminID]=" + AdminID);
	}
	public void DeleteAdminUserGroupByAdminID(int AdminID)
	{
		this.conn.Execute("DELETE From [HL_AdminUserGroup] Where AdminID=" + AdminID);
	}
	public DataTable GetAdminByAdminName(string AdminName)
	{
		IDbDataParameter dbDataParameter = this.conn.CreateParameter("@UserName", DbType.String, AdminName);
		return this.conn.GetDataTable("Select * From [HL_Admin] Where ([UserName] = @UserName)", new IDbDataParameter[]
		{
			dbDataParameter
		});
	}
	public bool AddAdmin(string UserName, string PassWord, int Access, string AcceptIP, int LoginTimes, DateTime LastLogin, string LastLoginIP, DateTime LoginDate, string LoginIP, string SecurityKey, bool Disable, DateTime EndDate, string Remark, string GropIDS, string Power, string ProvinceArea, string CityArea)
	{
		IDbDataParameter[] parameters = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@UserName", DbType.String, UserName),
			this.conn.CreateParameter("@PassWord", DbType.String, PassWord),
			this.conn.CreateParameter("@Access", DbType.Int32, Access),
			this.conn.CreateParameter("@AcceptIP", DbType.String, AcceptIP),
			this.conn.CreateParameter("@LoginTimes", DbType.Int32, LoginTimes),
			this.conn.CreateParameter("@LastLogin", DbType.DateTime, LastLogin),
			this.conn.CreateParameter("@LastLoginIP", DbType.String, LastLoginIP),
			this.conn.CreateParameter("@LoginDate", DbType.DateTime, LoginDate),
			this.conn.CreateParameter("@LoginIP", DbType.String, LoginIP),
			this.conn.CreateParameter("@SecurityKey", DbType.String, SecurityKey),
			this.conn.CreateParameter("@Disable", DbType.Boolean, Disable),
			this.conn.CreateParameter("@EndDate", DbType.DateTime, EndDate),
			this.conn.CreateParameter("@Remark", DbType.String, Remark),
			this.conn.CreateParameter("@Power", DbType.String, Power),
			this.conn.CreateParameter("@ProvinceArea", DbType.String, ProvinceArea),
			this.conn.CreateParameter("@CityArea", DbType.String, CityArea)
		};
		this.conn.BeginTransaction();
		int num;
		bool result;
		try
		{
			this.conn.ExecuteTransaction("INSERT INTO [HL_Admin] ([UserName], [PassWord], [LastLogin], [LastLoginIP], [LoginTimes], [AcceptIP], [SecurityKey], [LoginDate], [LoginIP], [Disable], [EndDate], [Access], [Remark], [Power],[ProvinceArea],[CityArea]) VALUES (@UserName, @PassWord, @LastLogin, @LastLoginIP, @LoginTimes, @AcceptIP, @SecurityKey, @LoginDate, @LoginIP, @Disable, @EndDate, @Access, @Remark, @Power,@ProvinceArea,@CityArea)", parameters);
			this.conn.m_transCommand.CommandText = "Select Admin_ID From [HL_Admin] Where ([UserName] = @UserName)";
			num = (int)this.conn.m_transCommand.ExecuteScalar();
		}
		catch
		{
			this.conn.Rollback();
			result = false;
			return result;
		}
		if (GropIDS != "" && GropIDS != null)
		{
			string[] array = GropIDS.Split(new char[]
			{
				','
			});
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i] != string.Empty)
				{
					try
					{
						this.conn.ExecuteTransaction(string.Concat(new object[]
						{
							"Insert Into [hl_AdminUserGroup](AdminID,GroupID) Values(",
							num,
							",",
							array[i],
							")"
						}));
					}
					catch
					{
						this.conn.Rollback();
						this.conn.Execute("DELETE From [HL_Admin] Where Admin_ID=" + num);
						result = false;
						return result;
					}
				}
			}
		}
		this.conn.Commit();
		result = true;
		return result;
	}
	public bool UpdateAdmin(int Admin_ID, string UserName, string PassWord, string LastLoginIP, string SecurityKey, string AcceptIP, bool Disable, DateTime EndDate, int LoginTimes, DateTime LastLogin, DateTime LoginDate, string LoginIP, int Access, string Remark, string GropIDS, string Power, string ProvinceArea, string CityArea)
	{
		if (LastLoginIP == null)
		{
			LastLoginIP = "127.0.0.1";
		}
		if (AcceptIP == null)
		{
			AcceptIP = "127.0.0.1";
		}
		bool result;
		if (PassWord == "" || PassWord == null)
		{
			DataTable dataTable = this.conn.GetDataTable("Select PassWord, SecurityKey From [HL_Admin] Where Admin_ID=" + Admin_ID);
			if (dataTable.Rows.Count == 0)
			{
				result = false;
				return result;
			}
			PassWord = dataTable.Rows[0]["PassWord"].ToString();
			SecurityKey = dataTable.Rows[0]["SecurityKey"].ToString();
		}
		IDbDataParameter[] parameters = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@UserName", DbType.String, UserName),
			this.conn.CreateParameter("@PassWord", DbType.String, PassWord),
			this.conn.CreateParameter("@Access", DbType.Int32, Access),
			this.conn.CreateParameter("@AcceptIP", DbType.String, AcceptIP),
			this.conn.CreateParameter("@LoginTimes", DbType.Int32, LoginTimes),
			this.conn.CreateParameter("@LastLogin", DbType.DateTime, LastLogin),
			this.conn.CreateParameter("@LastLoginIP", DbType.String, LastLoginIP),
			this.conn.CreateParameter("@LoginDate", DbType.DateTime, LoginDate),
			this.conn.CreateParameter("@LoginIP", DbType.String, LoginIP),
			this.conn.CreateParameter("@SecurityKey", DbType.String, SecurityKey),
			this.conn.CreateParameter("@Disable", DbType.Boolean, Disable),
			this.conn.CreateParameter("@EndDate", DbType.DateTime, EndDate),
			this.conn.CreateParameter("@Remark", DbType.String, Remark),
			this.conn.CreateParameter("@Power", DbType.String, Power),
			this.conn.CreateParameter("@Original_Admin_ID", DbType.Int32, Admin_ID),
			this.conn.CreateParameter("@ProvinceArea", DbType.String, ProvinceArea),
			this.conn.CreateParameter("@CityArea", DbType.String, CityArea)
		};
		this.conn.BeginTransaction();
		try
		{
			this.conn.ExecuteTransaction("DELETE From [HL_AdminUserGroup] Where AdminID=" + Admin_ID.ToString());
		}
		catch
		{
			this.conn.Rollback();
			result = false;
			return result;
		}
		string[] array = GropIDS.Split(new char[]
		{
			','
		});
		for (int i = 0; i < array.Length; i++)
		{
			if (array[i] != string.Empty)
			{
				try
				{
					this.conn.ExecuteTransaction(string.Concat(new string[]
					{
						"Insert Into [HL_AdminUserGroup] values(",
						Admin_ID.ToString(),
						",",
						array[i],
						",'' )"
					}));
				}
				catch
				{
					this.conn.Rollback();
					result = false;
					return result;
				}
			}
		}
		try
		{
			this.conn.ExecuteTransaction("UPDATE [HL_Admin] SET [UserName] = @UserName, [PassWord] = @PassWord, [LastLogin] = @LastLogin, [LastLoginIP] = @LastLoginIP, [LoginTimes] = @LoginTimes, [AcceptIP] = @AcceptIP, [SecurityKey] = @SecurityKey, [LoginDate] = @LoginDate, [LoginIP] = @LoginIP, [Disable] = @Disable, [EndDate] = @EndDate, [Access] = @Access, [Remark] = @Remark, [Power] = @Power, [ProvinceArea] = @ProvinceArea,[CityArea] = @CityArea WHERE (([Admin_ID] = @Original_Admin_ID))", parameters);
		}
		catch
		{
			this.conn.Rollback();
			result = false;
			return result;
		}
		this.conn.Commit();
		result = true;
		return result;
	}
	public bool DeleteAdmin(int Admin_ID)
	{
		this.conn.BeginTransaction();
		bool result;
		try
		{
			this.conn.ExecuteTransaction("DELETE From [HL_Admin] Where Admin_ID=" + Admin_ID);
			this.conn.ExecuteTransaction("DELETE From [HL_AdminUserGroup] where AdminID=" + Admin_ID);
		}
		catch
		{
			this.conn.Rollback();
			result = false;
			return result;
		}
		this.conn.Commit();
		result = true;
		return result;
	}
	public DataTable GetAdminByGroupID(int AdminGroupID)
	{
		return this.conn.GetDataTable("Select * From [HL_Admin] Where Admin_ID in(Select AdminID From [HL_AdminUserGroup] Where GroupID=" + AdminGroupID + ")");
	}
	public int ChangePassword(int AdminID, string OldPassWord, string NewPassWord)
	{
		int result;
		if (AdminID > 0)
		{
			DataTable dataTable = this.conn.GetDataTable("Select PassWord, SecurityKey From [HL_Admin] Where Admin_ID=" + AdminID);
			if (dataTable.Rows.Count == 1)
			{
				string a = dataTable.Rows[0]["PassWord"].ToString();
				string skey = dataTable.Rows[0]["SecurityKey"].ToString();
				string b = this.CreatePassWord(OldPassWord, skey);
				if (a == b)
				{
					string text = this.CreatePassWord(NewPassWord, skey);
					if (this.conn.Execute(string.Concat(new object[]
					{
						"UPDATE [HL_Admin] Set PassWord='",
						text,
						"' Where Admin_ID=",
						AdminID
					})) == 1)
					{
						result = 0;
					}
					else
					{
						result = 3;
					}
				}
				else
				{
					result = 2;
				}
			}
			else
			{
				result = 1;
			}
		}
		else
		{
			result = 1;
		}
		return result;
	}
	public string CreatePassWord(string pwd, string skey)
	{
		return FormsAuthentication.HashPasswordForStoringInConfigFile(FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "md5").ToUpper() + skey, "md5");
	}
}
